package com.ruoyi.file.service;

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.ruoyi.file.utils.AliOssUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.UUID;

/**
 * @author grx
 * @Classname AliOssService
 * @Description TODO
 * @Date 2025/3/6 17:02
 * @Version V1.0
 */



/**
 * @author grx
 * @Classname AliOssUtil
 * @Description TODO
 * @Date 2025/3/6 10:52
 * @Version V1.0
 */
@Primary
@Service
public class AliOssService implements ISysFileService {

    @Override
    public String uploadFile(MultipartFile file) throws Exception {
        //原始文件名
        String originalFilename = file.getOriginalFilename();
        //截取原始文件名的后缀   dfdfdf.png
        String extension = originalFilename.substring(originalFilename.lastIndexOf("."));
        //构造新文件名称
        String objectName = UUID.randomUUID().toString() + extension;

        //文件的请求路径
        String filePath = AliOssUtil.upload(file.getBytes(), objectName);
        return filePath;
    }
}